package com.example.lib5.linkedList.test;

/**
 * 创建日期：2022/6/9 0009 12:10
 *
 * @author tony.sun
 * 类说明：
 */
public class JosephuB {
    public static void main(String[] args) {
        int m=3,n=4;
        CircleLinkedList circleLinkedList = new CircleLinkedList();
        circleLinkedList.addBoy(5);
        BoyB count = circleLinkedList.count(2, 5);
        System.out.println(count);
    }
}
//节点
 class BoyB {
    int no;
    public BoyB Next;

    public BoyB(int no) {
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public BoyB getNext() {
        return Next;
    }

    public void setNext(BoyB next) {
        Next = next;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                '}';
    }
}
class CircleLinkedList{
    BoyB first=null;
    public void addBoy(int total) {
        //判断输入的值是否合理
        if (total<=0) {
            System.out.println("输入的值不合理");
            return;
        }
        BoyB curBoy=null;
        for (int i = 1; i < total+1; i++) {
            //如果是第一个数
            BoyB boyB = new BoyB(i);
            if (first==null) {
                first=boyB;
                first.setNext(first);
                curBoy=first;
            }else {
                curBoy.setNext(boyB);
                boyB.setNext(first);
                curBoy=boyB;
            }
        }
    }
    public void addBoy2(int nums){
        //判断是否合理 合里遍历  是否为null节点
        if (nums<=0) {System.out.println("输入的值有误"); return;}
        BoyB curBoy=null;
        for (int i = 1; i < nums+1; i++) {
            BoyB boyB = new BoyB(i);
            if (first==null){
                first=boyB;
                boyB.setNext(first);
                curBoy=boyB;
            }else {
                curBoy.setNext(boyB);
                boyB.setNext(first);
                curBoy=boyB;

            }
        }
    }

    public BoyB count( int count, int nums) {
        //判断是否合理
        if (count<=0||nums<=0||first==null) {
            System.out.println("输入不合理或者链表为空");
            return null;
        }
        BoyB helper=first;
        while (helper.getNext()!=first){
            helper=helper.getNext();
        }

        while (true){
            if (first==helper) {
                return first;
            }
            //删除前 先后移
            for (int i=0;i<count-1;i++){
                first=first.getNext();
                helper=first.getNext();
            }

            first=first.getNext();
            helper.setNext(first);
        }

    }
}
